package com.base.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.sqlparam.CallbackinfoesExamplePage;
import com.base.po.voip.Callbackinfoes;
import com.base.po.voip.CallbackinfoesExample.Criteria;
import com.base.service.CallbackinfoesService;
import com.base.service.LogsService;
import com.base.utils.DateUtil;
import com.base.utils.IP;
import com.base.utils.Page;
import com.base.utils.Produce;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_huadanxiangqing {
	@Autowired
	private CallbackinfoesService callbackinfoesService;
	@Autowired	
	private LogsService logsService;// log日志

	// 话单详情
	@RequestMapping("/content_huadanxiangqingOld")
	public ModelAndView indexOld(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		CallbackinfoesExamplePage examplePage = new CallbackinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CallTime");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Callbackinfoes> callbackinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		switch (adminUsergrade) {
		case 1:
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		case 2:
			examplePage.getOredCriteria().add(criteria.andShopidEqualTo(adminUserId));
			break;
		}/*
			 * 设置分页
			 */
		int totlenum = callbackinfoesService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", 1);// 传入session中
		request.setAttribute("date", "01/01/2017 -  ");
		request.setAttribute("adminUsergrade", adminUsergrade);
		callbackinfoes = callbackinfoesService.selectByExamplePage(examplePage);
		if (callbackinfoes != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huadanxiangqing");
			request.setAttribute("list", callbackinfoes);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "话单详情", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	@RequestMapping("/selectCallbackinfoesHOld")
	public ModelAndView selectCallbackinfoesHOld(HttpServletRequest request, String id, String caller, String call164,
			Integer curPage, String date) throws Exception {
		// 设置session缓存
		if (date == null) {
			if (request.getSession().getAttribute("date") != null) {
				date = (String) request.getSession().getAttribute("date");
			} else {
				date = "01/01/2017 -  ";
			}
		}
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (caller == null) {
			if (request.getSession().getAttribute("caller") != null) {
				caller = (String) request.getSession().getAttribute("caller");
			}
		}
		if (call164 == null) {
			if (request.getSession().getAttribute("call164") != null) {
				call164 = (String) request.getSession().getAttribute("call164");
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		CallbackinfoesExamplePage examplePage = new CallbackinfoesExamplePage();
		examplePage.setOrderByClause("CallTime");
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Callbackinfoes> users = null;
		Criteria criteria = examplePage.createCriteria();
		// 条件筛选
		if (id != null && !id.equals("")) {
			request.setAttribute("id", id);
			criteria = criteria.andIdEqualTo(id);
			examplePage.getOredCriteria().add(criteria);
		}
		if (caller != null && !caller.equals("")) {
			request.setAttribute("caller", caller);
			criteria = criteria.andCallerLike("%" + caller + "%");
			examplePage.getOredCriteria().add(criteria);
		}
		if (call164 != null && !call164.equals("")) {
			request.setAttribute("call164", call164);
			criteria = criteria.andCalle164Like("%" + call164 + "%");
			examplePage.getOredCriteria().add(criteria);
		}
		// 设置时间
		String[] sourceStrArray = date.trim().split("-");
		Date dateFrom = null;
		Date dateTo = null;
		for (int i = 0; i < sourceStrArray.length; i++) {
			if (dateFrom == null) {
				dateFrom = DateUtil.strToDateLong(sourceStrArray[i]);
			} else {
				dateTo = DateUtil.strToDateLong(sourceStrArray[i]);
			}
		}
		if (dateFrom != null) {
			request.setAttribute("date", date);
			if (dateTo != null) {
				criteria = criteria.andCalltimeBetween(dateFrom, dateTo);
			} else {
				criteria = criteria.andCalltimeBetween(dateFrom, Produce.getNowDate());
			}
			examplePage.getOredCriteria().add(criteria);
		}
		switch (adminUsergrade) {
		case 1:
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		case 2:
			examplePage.getOredCriteria().add(criteria.andShopidEqualTo(adminUserId));
			break;
		}

		/*
		 * 设置分页
		 */
		int totlenum = callbackinfoesService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", curPage);// 当前页面传入session中

		users = callbackinfoesService.selectByExamplePage(examplePage);
		if (users != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huadanxiangqing");
			request.setAttribute("list", users);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/content_huadanxiangqing")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Callbackinfoes> callbackinfoes = null;
		
		switch (adminUsergrade) {
		case 0:
			page.setCurrentPageNo(1);
			page.setRecordTotal(callbackinfoesService.countByAll(null, null, null, null));
			callbackinfoes = callbackinfoesService.selectByAll(page, null, null, null, null);
			break;
		case 1:
			page.setCurrentPageNo(1);
			page.setRecordTotal(callbackinfoesService.countByParentID(adminUserId, null, null, null, null));
			callbackinfoes = callbackinfoesService.selectByParentID(page, adminUserId, null, null, null, null);
			break;

		case 2:
			page.setCurrentPageNo(1);
			page.setRecordTotal(callbackinfoesService.countByShopID(adminUserId, null, null, null, null));
			callbackinfoes = callbackinfoesService.selectByShopID(page, adminUserId, null, null, null, null);
			break;
		}

		BigDecimal OMoney = callbackinfoesService.sumByParentID(adminUserId, null, null, null, null);

		/*
			 * 设置分页
			 */
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
		request.setAttribute("date", "01/01/2018 -  ");
		request.setAttribute("adminUsergrade", adminUsergrade);
		request.setAttribute("adminUserId", adminUserId);
		request.setAttribute("OMoney", OMoney);
		
		if (callbackinfoes != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huadanxiangqing");
			request.setAttribute("list", callbackinfoes);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "话单详情", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/selectCallbackinfoesH")
	public ModelAndView selectCallbackinfoesH(HttpServletRequest request, String id, String caller, String call164,
			Integer curPage, String date) throws Exception {
		// 设置session缓存
		if (date == null) {
			if (request.getSession().getAttribute("date") != null) {
				date = (String) request.getSession().getAttribute("date");
			} else {
				date = "01/01/2018 -  ";
			}
		}
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (caller == null) {
			if (request.getSession().getAttribute("caller") != null) {
				caller = (String) request.getSession().getAttribute("caller");
			}
		}
		if (call164 == null) {
			if (request.getSession().getAttribute("call164") != null) {
				call164 = (String) request.getSession().getAttribute("call164");
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Callbackinfoes> callbackinfoes = null;
		
		String fCaller = null;
		String fCall164 = null;
		String fDateFrom = null;
		String fDateTo = null;
		// 条件筛选
		if (caller != null && !caller.equals("")) {
			fCaller = caller;
		}
		if (call164 != null && !call164.equals("")) {
			fCall164 = call164;
		}
		// 设置时间
		String[] sourceStrArray = date.trim().split("-");
		Date dateFrom = null;
		Date dateTo = null;
		for (int i = 0; i < sourceStrArray.length; i++) {
			if (dateFrom == null) {
				dateFrom = DateUtil.strToDateLong(sourceStrArray[i]);
			} else {
				dateTo = DateUtil.strToDateLong(sourceStrArray[i]);
			}
		}
		if (dateFrom != null) {
			fDateFrom = new SimpleDateFormat("yyyy-MM-dd").format(dateFrom) +" 00:00:00";
		}
		if (dateTo != null) {
			fDateTo = new SimpleDateFormat("yyyy-MM-dd").format(dateTo) +" 23:59:59";
		}
		switch (adminUsergrade) {
		case 0:
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(callbackinfoesService.countByAll(fCaller, fCall164, fDateFrom, fDateTo));
			callbackinfoes = callbackinfoesService.selectByAll(page, fCaller, fCall164, fDateFrom, fDateTo);
			break;
		case 1:
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(callbackinfoesService.countByParentID(adminUserId, fCaller, fCall164, fDateFrom, fDateTo));
			callbackinfoes = callbackinfoesService.selectByParentID(page, adminUserId, fCaller, fCall164, fDateFrom, fDateTo);
			break;

		case 2:
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(callbackinfoesService.countByShopID(adminUserId, fCaller, fCall164, fDateFrom, fDateTo));
			callbackinfoes = callbackinfoesService.selectByShopID(page, adminUserId, fCaller, fCall164, fDateFrom, fDateTo);
			break;
		}

		BigDecimal OMoney = callbackinfoesService.sumByParentID(adminUserId, fCaller, fCall164, fDateFrom, fDateTo);

		/*
		 * 设置分页
		 */
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		request.setAttribute("adminUsergrade", adminUsergrade);
		request.setAttribute("adminUserId", adminUserId);
		request.setAttribute("OMoney", OMoney);
		if (callbackinfoes != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huadanxiangqing");
			request.setAttribute("caller", caller);
			request.setAttribute("call164", call164);
			request.setAttribute("date", date);
			request.setAttribute("list", callbackinfoes);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
